package com.streamer.app.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.streamer.app.R
import com.streamer.app.bean.CoursewareInfoBean

/**
 * 课件列表适配器
 */
class CoursewareAdapter(
    private val coursewareList: MutableList<CoursewareInfoBean>,
    private val onItemAction: (CoursewareInfoBean, Int, String) -> Unit
) : RecyclerView.Adapter<CoursewareAdapter.CoursewareViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CoursewareViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_course_ware, parent, false)
        return CoursewareViewHolder(view)
    }

    override fun onBindViewHolder(holder: CoursewareViewHolder, position: Int) {
        val courseware = coursewareList[position]
        holder.bind(courseware)
    }

    override fun getItemCount(): Int = coursewareList.size

    /**
     * 更新数据
     */
    fun updateData(newList: List<CoursewareInfoBean>) {
        coursewareList.clear()
        coursewareList.addAll(newList)
        notifyDataSetChanged()
    }

    /**
     * 添加单个项目
     */
    fun addItem(courseware: CoursewareInfoBean) {
        coursewareList.add(courseware)
        notifyItemInserted(coursewareList.size - 1)
    }

    /**
     * 删除项目
     */
    fun removeItem(position: Int) {
        if (position in 0 until coursewareList.size) {
            coursewareList.removeAt(position)
            notifyItemRemoved(position)
        }
    }

    /**
     * 根据ID删除项目
     */
    fun removeItemById(id: String) {
        val position = coursewareList.indexOfFirst { it.id == id }
        if (position != -1) {
            removeItem(position)
        }
    }

    inner class CoursewareViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val ivIcon: ImageView = itemView.findViewById(R.id.iv_courseware_icon)
        private val tvName: TextView = itemView.findViewById(R.id.tv_courseware_name)
        private val tvDate: TextView = itemView.findViewById(R.id.tv_courseware_date)
        private val tvSize: TextView = itemView.findViewById(R.id.tv_courseware_size)
        private val ivPlay: ImageView = itemView.findViewById(R.id.iv_play_courseware)
        private val ivDelete: ImageView = itemView.findViewById(R.id.iv_delete_courseware)

        fun bind(courseware: CoursewareInfoBean) {
            // 设置文件信息
            tvName.text = courseware.fileName
            tvDate.text = courseware.createTime
            var fileType = courseware.fileName.substringAfterLast('.')
            when (fileType) {
                "pdf" -> {
                    ivIcon.apply {
                        Glide.with(this).load(R.mipmap.pdf_icon).into(this)
                    }
                }

                "doc" -> {
                    ivIcon.apply {
                        Glide.with(this).load(R.mipmap.doc_icon).into(this)

                    }
                }

                "docx" -> {
                    ivIcon.apply {
                        Glide.with(this).load(R.mipmap.docx_icon).into(this)

                    }
                }

                "ppt" -> {
                    ivIcon.apply {
                        Glide.with(this).load(R.mipmap.ppt_icon).into(this)

                    }
                }

                "pptx" -> {
                    ivIcon.apply {
                        Glide.with(this).load(R.mipmap.pptx_icon).into(this)

                    }
                }

                "jpeg" -> {
                    ivIcon.apply {
                        Glide.with(this).load(R.mipmap.jpeg_icon).into(this)

                    }
                }

                "png" -> {
                    ivIcon.apply {
                        Glide.with(this).load(R.mipmap.png_icon).into(this)

                    }
                }
            }

            // 设置点击事件
            itemView.setOnClickListener {
                onItemAction(courseware, position, "select")
            }

            ivPlay.setOnClickListener {
                onItemAction(courseware, position, "play")
            }

            ivDelete.setOnClickListener {
                onItemAction(courseware, position, "delete")
            }

            // 长按事件（可以用于更多操作）
            itemView.setOnLongClickListener {
                onItemAction(courseware, position, "long_click")
                true
            }
        }
    }
}

